const crypto = require('crypto');
const request = require('request');

//请将 appId 修改为你的 appId，appid 为 number
var appId = 1048172193;
//请将 serverSecret 修改为你的 serverSecret，serverSecret 为 string
var serverSecret = "b86af07b8a280de3ed39fe2ab30b9b8f";

//生成签名
//Signature=md5(AppId + SignatureNonce + ServerSecret + Timestamp)
function GenerateUASignature(appId, signatureNonce, serverSecret, timeStamp){
    const hash = crypto.createHash('md5'); //规定使用哈希算法中的MD5算法
    var str = appId + signatureNonce + serverSecret + timeStamp;
    hash.update(str);
    //hash.digest('hex')表示输出的格式为16进制
    return hash.digest('hex');
}

var signatureNonce = crypto.randomBytes(8).toString('hex');
var timeStamp = Math.round(Date.now()/1000);
var sig = GenerateUASignature(appId, signatureNonce, serverSecret, timeStamp)

//以下示例可获取 RoomID 为 room1 的房间用户数
var url = `https://rtc-api.zego.im/?Action=DescribeUserNum&RoomId[]=room1&AppId=${appId}&SignatureNonce=${signatureNonce}&Timestamp=${timeStamp}&Signature=${sig}&SignatureVersion=2.0&IsTest=false`;

request(url, function(error, response, body){
  console.log('Url: ',url)
  console.log('StatusCode: ',response.statusCode)
  console.log('Error: ', error)
  if(!error && response.statusCode){
      console.log(body)
  }
})
